package com.zpark.controller;

import com.zpark.exception.InsertBooksException;
import com.zpark.exception.SelectByIdBooksException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.servlet.ModelAndView;

/**
 * 全局异常处理
 */
@ControllerAdvice
public class GlobalExceptionController {

    /*@ExceptionHandler
    public JsonUtils<String> globalHandlerException(Exception e){
        if (e instanceof InsertBooksException) {
            return new JsonUtils<String>()
        }
    }*/

    @ExceptionHandler
    public ModelAndView globalHandlerException(Exception e){
        ModelAndView vm = new ModelAndView();
        vm.setViewName("error");
        if (e instanceof InsertBooksException) {
            vm.addObject("msg", e.getMessage());
            return vm;
        }else if (e instanceof SelectByIdBooksException) {
            vm.addObject("msg", e.getMessage());
            return vm;
        }

        return vm;
    }
}

